﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class ProductClassService: RepositoryBase
    {
        public List<ProductClass> GetAllClassWithSelectOne()
        {
            List<ProductClass> list = new List<ProductClass>();
            list.Add(new ProductClass { ID = 0, Name = "请选择产品分类" });
            list.AddRange(GetEntities<ProductClass>().ToList());
            return list;
        }

        public List<ProductClass> GetAllClassWithSelectOneByUser(User user)
        {
            List<ProductClass> list = new List<ProductClass>();
            list.Add(new ProductClass { ID = 0, Name = "请选择产品分类" });
            if (user.UserType.ID == (int)RoleType.InternalCustomer)
            {
                list.AddRange(GetEntities<ProductClass>());
            }
            else
            {
                list.AddRange(GetEntities<ProductClass>().Where(c => !c.IsOEM).ToList());
                if (!user.ProductClassReference.IsLoaded)
                {
                    user.ProductClassReference.Load();
                }
                if (user.ProductClass != null)
                {
                    list.AddRange(GetEntities<ProductClass>().Where(c => c.ID == user.ProductClass.ID));
                }
            }
            return list;
        }

        public List<ProductClass> GetAllClassIncludeOEMWithSelectOneByUser(User user)
        {
            List<ProductClass> list = new List<ProductClass>();
            list.Add(new ProductClass { ID = 0, Name = "请选择产品分类" });
            if (user.UserType.ID == (int)RoleType.InternalCustomer)
            {
                list.AddRange(GetEntities<ProductClass>());
            }
            else
            {
                list.AddRange(GetEntities<ProductClass>().ToList());
                if (!user.ProductClassReference.IsLoaded)
                {
                    user.ProductClassReference.Load();
                }
                if (user.ProductClass != null)
                {
                    list.AddRange(GetEntities<ProductClass>().Where(c => c.ID == user.ProductClass.ID));
                }
            }
            return list;
        }

        public List<ProductClass> GetAllClass()
        {
            List<ProductClass> list = new List<ProductClass>();
            list.AddRange(GetEntities<ProductClass>().ToList());
            return list;
        }

        public List<ProductClass> GetOEMProdutClassWithSelectOne()
        {
            List<ProductClass> list = new List<ProductClass>();
            list.Add(new ProductClass { ID = 0, Name = "请选择OEM产品类型" });
            list.AddRange(GetEntities<ProductClass>().Where(c=>c.IsOEM).ToList());
            return list;
        }

        public List<ProductType> GetTypeListWithSelectOne(int classID)
        {
            List<ProductType> typeList = new List<ProductType>();
            typeList.Add(new ProductType { ProductTypeID = 0, ProductTypeName = "请选择产品型号" });
            typeList.AddRange(GetEntities<ProductType>().Where(p => p.ProductClass.ID == classID).ToList());            
            return typeList;
        }

        public List<ProductType> GetAllProductType(string keyword)
        {
            List<ProductType> typeList = new List<ProductType>();
            IQueryable<ProductType> entities;
            if (!string.IsNullOrEmpty(keyword))
            {
                entities = GetEntities<ProductType>().Where(t=>t.ProductTypeName.Contains(keyword)).OrderBy(p => p.ProductClass.Name);
            }
            else
            {
                entities = GetEntities<ProductType>().OrderBy(p => p.ProductClass.Name);
            }
            foreach (ProductType entity in entities)
            {
                if (!entity.ProductClassReference.IsLoaded)
                {
                    entity.ProductClassReference.Load();
                }
                typeList.Add(entity);
            }
            return typeList;
        }

        public ProductType GetProductType(int typeID)
        {
            ProductType type = GetEntities<ProductType>().Where(t => t.ProductTypeID == typeID).FirstOrDefault();
            if (!type.ProductClassReference.IsLoaded)
            {
                type.ProductClassReference.Load();
            }
            return type;
        }

        public ProductType GetProductTypeByName(string productType)
        {
            ProductType type = GetEntities<ProductType>().Where(t => t.ProductTypeName.Equals(productType)).FirstOrDefault();

            if (type != null && !type.ProductClassReference.IsLoaded)
            {
                type.ProductClassReference.Load();
            }
            return type;
        }

        public void AddProductType(ProductType productType)
        {
            InsertEntity(productType);
        }

        public void UpdateProductType(ProductType productType)
        {
            UpdateEntity(productType);
        }

        public void UpdateProductClass(ProductClass productClass)
        {
            UpdateEntity(productClass);
        }

        public ProductClass GetProductClass(int classID)
        {
            return GetEntities<ProductClass>().Where(p => p.ID == classID).FirstOrDefault();
        }

        public IQueryable<ProductVersion> GetVersions()
        {
            return GetEntities<ProductVersion>().OrderBy(v => v.Name);
        }

        public ProductVersion GetVersionByID(int versionID)
        {
            return GetEntities<ProductVersion>(p => p.ID == versionID).FirstOrDefault();
        }

        public void DeleteVersionByID(int versionID)
        {
            DeleteEntity<ProductVersion>(p => p.ID == versionID, Guid.Empty);
        }

        public void UpdateVersion(ProductVersion version)
        {
            UpdateEntity(version);
        }

        public void AddNewProductVersion(ProductVersion version)
        {
            InsertEntity(version);
        }

        public bool IsExistedVersion(string textName)
        {
            bool flug = false;
            if (GetEntities<ProductVersion>(p => p.Name == textName).Count() > 0)
            {
                flug = true;
            }

            return flug;
        }

        public bool IsExistedVersionEdit(string textName)
        {
            bool flug = false;
            if (GetEntities<ProductVersion>(p => p.Name == textName).Count() > 1)
            {
                flug = true;
            }

            return flug;
        }

        public List<ProductVersion> GetVersionsWithSelectOne()
        {
            List<ProductVersion> versionList = new List<ProductVersion>();
            versionList.Add(new ProductVersion { ID=0, Name="请选择" });

            IQueryable<ProductVersion> versions = GetEntities<ProductVersion>().OrderBy(v => v.Name);
            foreach (ProductVersion version in versions)
            {
                versionList.Add(version);
            }

            return versionList;
        }

        public void AddProductClass(ProductClass productClass)
        {
            InsertEntity(productClass);
        }

        public List<ProductType> GetProductTypeByProductClassID(int productClassID)
        {
            return GetEntities<ProductType>().Where(p => p.ProductClass.ID == productClassID).ToList();
        }

        public void DeleteProductTypeByID(int productTypeID)
        {
            // 检查是否该产品型号是否被用到,(所有有外键关系的表)
            ProviderService providerService = new ProviderService();
            List<Document> documents = providerService.GetDocumentByProductTypeID(productTypeID);
            if (null != documents && documents.Count() > 0)
            {
                throw new Exception("该产品型号已经被使用");
            }

            SparePartService sparePartService = new SparePartService();
            List<SparePart> spareParts = sparePartService.GetSparePartByProductTypeID(productTypeID);
            if (null != spareParts && spareParts.Count() > 0)
            {
                throw new Exception("该产品型号已经被使用");
            }

            ApplicationProductService appProductService = new ApplicationProductService();
            List<ApplicationProduct> appProducts = appProductService.GetAppProductByProductTypeID(productTypeID);
            if (null != appProducts && appProducts.Count() > 0)
            {
                throw new Exception("该产品型号已经被使用");
            }

            List<ApplicationProductHistory> appProductHistorys = appProductService.GetAppProductHistoryByProductTypeID(productTypeID);
            if (null != appProductHistorys && appProductHistorys.Count() > 0)
            {
                throw new Exception("该产品型号已经被使用");
            }

            DeleteEntity<ProductType>(s => s.ProductTypeID == productTypeID, Guid.Empty);
        }

        public void DeleteProductClassByID(int productClassID)
        {
            // 检查是否该产品型号是否被用到,(所有有外键关系的表)
            ProviderService providerService = new ProviderService();
            List<Provider> providers = providerService.GetProviderByProductClassID(productClassID);
            if (null != providers && providers.Count() > 0)
            {
                throw new Exception("该产品类型已经被使用");
            }

            UserService userService = new UserService();
            List<User> users = userService.GetUserByProductClassID(productClassID);
            if (null != users && users.Count() > 0)
            {
                throw new Exception("该产品类型已经被使用");
            }

            ProductService productService = new ProductService();
            List<Product> products = productService.GetProductByProductClassID(productClassID);
            if (null != products && products.Count() > 0)
            {
                throw new Exception("该产品类型已经被使用");
            }

            List<ProductType> productTypes = this.GetProductTypeByProductClassID(productClassID);
            if (null != productTypes && productTypes.Count() > 0)
            {
                throw new Exception("该产品类型已经被使用");
            }

            DeleteEntity<ProductClass>(s => s.ID == productClassID, Guid.Empty);
        }
    }
}
